package day13;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//可缓存线程池
public class Demo {
    public static void main(String[] args) {
//        ExecutorService es= Executors.newCachedThreadPool();
//        for(int i=0;i<100;i++){
//            es.execute(()->{
//                System.out.println(Thread.currentThread().getName());
//            });
//        }
//        es.shutdown();
        String[] names = {"红烧鸡脚", "甲鱼", "红烧猪蹄", "正宗回锅肉", "重庆火锅","a","b","c","d"};
        long[] times = {1000, 3000, 5000, 5000, 2000,3000,1000,2000,6000};
        ExecutorService es = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        for (int i = 0; i < names.length; i++) {
            int index = i;
            es.execute(() -> {
                System.out.println("哇咔咔开始做菜咯:" + names[index]);
                try {
                    Thread.sleep(times[index]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("哇咔咔做好了(#`O′):" + names[index]);
            });
        }
        es.shutdown();
        //结束的时间一定是线程池关闭了 并且里面的任务也结束了
        //用于判断线程池里面的任务是否执行结束了
        //是一个布尔值 如果结束返回true 没有结束则为false
        while (true) {
            if (es.isTerminated()) {
                long end = System.currentTimeMillis();
                System.out.println("所有菜都炒好了！快大吃一顿" + (end - start) + "ms");
                break;
            }
        }
    }
}
